<?php
/*
 * @Author: lokei
 * @Date: 2022-09-19 13:45:10
 * @LastEditors: lokei
 * @LastEditTime: 2022-12-31 19:16:53
 * @Description: 
 */
namespace App\Http\Controllers\Activity;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Activity\ActivityModel;
use App\Models\Activity\RecordModel;
use App\Models\Pay\Card\CardRecordModel;
use App\Models\Pay\Card\PayCardModel;
use Illuminate\Http\Request;

class RecordController extends Controller
{

	public function list() {
		$items = RecordModel::orderBy('created_at', 'desc')->get();
		$res = ResultTool::success();
		$res['data']['items'] = $items;
		$res['data']['total'] = count($items);
		return $res;
	}

	public function join(Request $request) {
		$user = app('auth')->user();
		$is_exist = RecordModel::where('uid', '=', $user->uid)->where('activity_id', '=', $request->input('activity_id'))->first();
		if(!is_null($is_exist)) {
			$res = ResultTool::fail();
			$res['errorMsg'] = '已报名过，请不要重复报名';
			return $res;
		}
		$record = new RecordModel();
		$user_info = [
			'name' => $user->name ? $user->name : $user->nickname,
			'mobile' => $user->mobile
		];
		$record->uid = $user->uid;
		$record->user_info = json_encode($user_info);
		$record->activity_id = $request->input('activity_id');
		$activity = ActivityModel::where('id', '=', $request->input('activity_id'))->first();
		$activity_info = [
			'name' => $activity->name
		];
		$record->activity_info = json_encode($activity_info);
		$record->created_at = date('Y-m-d H:i:s', time());
		$record->save();
		$activity->join_count = $activity->join_count + 1;
		$activity->save();
		return ResultTool::success();
	}

	public function joinCard(Request $request)
	{
		$user = app('auth')->user();
		$pay_card_record = CardRecordModel::where('uid', '=', $user->uid)->where('card_id', '=', $request->input('pay_card_id'))->first();
		if(is_null($pay_card_record)) {
			$res = ResultTool::fail();
			$res['errorMsg'] = '尚未开通此类储值卡！';
			return $res;
		}
		$condition = [
			'uid' => $user->uid,
			'activity_id' => $request->input('activity_id'),
			'pay_card_id' => $request->input('pay_card_id')
		];
		$record = RecordModel::where($condition)->first();
		if(!is_null($record)) {
			$res = ResultTool::fail();
			$res['errorMsg'] = '已领取，请不要重复领取！';
			return $res;
		}
		$activity = ActivityModel::where('id', '=', $request->input('activity_id'))->first();
		if($activity->distributed_money >= $activity->total_money) {
			$res = ResultTool::fail();
			$res['errorMsg'] = '活动金额已经全部领取完毕！';
			return $res;
		}
		$record = new RecordModel();
		$user_info = [
			'name' => $user->name ? $user->name : $user->nickname,
			'mobile' => $user->mobile
		];
		$record->uid = $user->uid;
		$record->user_info = json_encode($user_info);
		$record->activity_id = $request->input('activity_id');
		$activity_info = [
			'name' => $activity->name
		];
		$record->activity_info = json_encode($activity_info);
		$record->pay_card_id = $request->input('pay_card_id');
		$pay_card = PayCardModel::where('id', '=', $request->input('pay_card_id'))->first();
		$pay_card_info = [
			'name' => $pay_card->name
		];
		$record->pay_card_info = json_encode($pay_card_info);
		$record->created_at = date('Y-m-d H:i:s', time());
		$pay_card_list = json_decode($activity->pay_card_list);
		foreach($pay_card_list as $pay_card) {
			if($pay_card->card_id == $request->input('pay_card_id')) {
				if($pay_card->type == '1') {
					if($pay_card->money >= ($activity->total_money - $activity->distributed_money)) {
						$record->money = $activity->total_money - $activity->distributed_money;
					} else {
						$record->money = $pay_card->money;
					}
				} else {
					$tmp_money = rand($pay_card->min_money, $pay_card->max_money);
					if($tmp_money >= ($activity->total_money - $activity->distributed_money)) {
						$record->money = $activity->total_money - $activity->distributed_money;
					} else {
						$record->money = $tmp_money;
					}
				}
				break;
			}
		}
		$record->save();
		$pay_card_record->money = $pay_card_record->money + $record->money;
		$pay_card_record->save();

		$activity->distributed_money = $activity->distributed_money + $record->money;
		$activity->join_count = $activity->join_count + 1;
		$activity->save();

		return ResultTool::success();
	}

	public function ofMe(Request $request) {
		$user = app('auth')->user();
		$items = RecordModel::where('uid', '=', $user->uid)->where('activity_id', '=', $request->input('activity_id'))->get();
		$res = ResultTool::success();
		$res['data']['items'] = $items;
		$res['data']['total'] = count($items);
		return $res;
	}

	public function delete(Request $request) {
		$record = RecordModel::where('id', '=', $request->input('id'))->first();
		$activity = ActivityModel::where('id', '=', $record->activity_id)->first();
		$activity->join_count = $activity->join_count - 1;
		$activity->save();
		RecordModel::where('id', '=', $request->input('id'))->delete();
		return ResultTool::success();
	}
}
